package com.story.rpc.handler;

import com.story.rpc.dto.RpcReq;
import com.story.rpc.provider.ServiceProvider;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Method;

@Slf4j
public class RpcReqHandler {
    private final ServiceProvider serviceProvider;

    public RpcReqHandler(ServiceProvider serviceProvider) {
        this.serviceProvider = serviceProvider;
    }

    @SneakyThrows
    public Object invoke(RpcReq request){
        String rpcServiceName = request.rpcServiceName();
        Object service = serviceProvider.getService(rpcServiceName);

        log.debug("获取到对应服务:{}",service.getClass().getCanonicalName());

        Method method = service.getClass().getMethod(request.getMethodName(), request.getParamTypes());
        method.invoke(service, request.getParams());
        return method.invoke(service, request.getParams());
    }
}
